# Use python as base image
FROM python:3.12-alpine3.22 AS base

# Change the working directory to the `app` directory
WORKDIR /app

FROM base AS builder

# Install uv
RUN apk add uv

# Set the environment variables
ENV UV_COMPILE_BYTECODE=1
ENV UV_LINK_MODE=copy

# Install dependencies
RUN --mount=type=cache,target=/root/.cache/uv \
    --mount=type=bind,source=uv.lock,target=uv.lock \
    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \
    --mount=type=bind,source=.python-version,target=.python-version \
    uv sync --no-dev --frozen --no-install-project --no-editable

# Copy the project into the image
ADD . /app

# Sync the project
RUN --mount=type=cache,target=/root/.cache/uv \
    uv sync --no-dev --frozen

FROM base AS release

# Copy the environment, but not the source code
COPY --from=builder --chown=app:app /app/.venv /app/.venv

# Copy the project into the image
COPY . .

# Set the path
ENV PATH="/app/.venv/bin:$PATH"

# Expose port
EXPOSE ${LISTEN_PORT:-3000}

# Run application
ENTRYPOINT [ "python", "-m", "app.main" ]
